<?php
/**
 * This file is form http://findcat.cn
 *
 * @link     https://findcat.cn
 * @email    1476982312@qq.com
 */
namespace App\Http\Controllers\Admin;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Traits\AdminHelp;
use App\Models\EveryDayContent;
use Encore\Admin\Auth\Database\Administrator;
use Encore\Admin\Controllers\AdminController;

class EveryDayContentController extends AdminController
{
    use AdminHelp;

    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '每日一句管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new EveryDayContent());

        $grid->column('id', 'ID')->sortable();
        $grid->column('title', '标题');
        $grid->column('content', '内容');
        $grid->column('status', '状态')->switch();
        $grid->column('admin_user_id', '操作人')->display(function ($value) {
            return Administrator::query()->find($value)->name ?? '';
        });
        $grid->column('created_at', '创建时间');

        $grid->expandFilter();
        $grid->setActionClass(Grid\Displayers\Actions::class);

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(EveryDayContent::query()->findOrFail($id));

        $show->field('id', 'ID');
        $show->field('title', '标题');
        $show->field('content', '内容');
        $show->field('status', '状态');
        $show->field('admin_user_id', '操作人');
        $show->field('created_at', '创建时间');
        $show->field('updated_at', '修改时间');

        return $show;
    }

    /**
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new EveryDayContent());

        $form->text('title', '标题');
        $form->textarea('content', '内容');
        $form->switch('status', '状态');

        $this->autoAdminUserSaving($form);

        return $form;
    }
}
